namespace :data do
  desc "导入默认数据"
  task :load=>:environment do
    [
      # basketball
      {:type=>"Basketball",:country=>"America",:name=>"NBA",:feed_url=>"url"},
      
      # football
      # England
      {:type=>"Football",:country=>"England",:name=>"Premier League"},
      {:type=>"Football",:country=>"England",:name=>"Championship(Division 1)"},
      {:type=>"Football",:country=>"England",:name=>"FA Cup"},
      {:type=>"Football",:conntry=>"England",:name=>"Carling Cup"},
      # Italy
      {:type=>"Football",:country=>"Italy",:name=>"Serie A"},
      # Gemany
      {:type=>"Football",:country=>"",:name=>""},
      {:type=>"Football",:country=>"",:name=>""},
      {:type=>"Football",:country=>"",:name=>""},
      {:type=>"Football",:country=>"",:name=>""},
      {:type=>"Football",:country=>"",:name=>""},
      {:type=>"Football",:country=>"",:name=>""}
    ]
  end


  desc "load translation table"
  task :load_translation_table=>:environment do
    file = File.join(Rails.root,"db","data","translation_table.csv")
    data = File.read(file)
    data.split("\n").each do |line|
      line_data = line.split(",")
      team_name1 = line_data[3].to_s.gsub(/\"/,'')
      team_name2 = line_data[4].to_s.gsub(/\"/,'')
      image = line_data.last.to_s.gsub(/\"/,'')
      # puts "team1: #{team1},team2: #{team2},image: #{image}"
      
      team1 = Team.find_by_name(team_name1)

      if team1
        team1.name_alias = team_name2
        team1.pic = "teams/#{image}"
        team1.save!
        puts "#{team_name1} -> #{team_name2}"
      end

    end
  end


  desc "create leagues,teams,sports from translation table"
  task :create_from_translation_table=>["clear:countries","clear:leagues","clear:teams","generate:sport"] do
    file = File.join(Rails.root,"db","data","translation_table.csv")
    data = File.read(file)
    data.split("\n").each do |line|
      line_data    = line.split(",")
      league_id    = line_data[0]
      country_name = line_data[1].to_s.gsub(/\"/,'')
      league_name  = line_data[2].to_s.gsub(/\"/,'')
      team_name1   = line_data[3].to_s.gsub(/\"/,'')
      team_name2   = line_data[4].to_s.gsub(/\"/,'')
      image        = line_data.last.to_s.gsub(/\"/,'').gsub(/[\n\r]{1,2}/,'')

      # country
      country = Country.find_or_create_by_name(country_name)
      country.save

      # league
      league = League.find_or_initialize_by_cid(league_id)
      league.name = league_name
      league.country = country
      if league_name == "NBA"
        league.sport = Sport.find_by_name("Basketball")
      else
        league.sport = Sport.find_by_name("Football")
      end
      league.save

      # team
      team = Team.find_or_initialize_by_name(team_name1)
      team.sport = league.sport
      team.name_alias = team_name2
      team.pic = "teams/" + image.to_s
      team.save

      

    end
  end

  desc "stat"
  task :stat_translation=>:environment do
    puts "teams count #{Team.count}"
    puts "translated teams count: #{Team.count :conditions=>"name_alias IS NOT NULL"}"
    puts "untranslated teams count: #{Team.count :conditions=>{:name_alias=>nil}}"
    puts "untranslated teams are:\n"
    puts Team.all(:conditions=>{:name_alias=>nil}).collect(&:name).inspect
  end


  desc "point config"
  task :point_config=>:environment do
    point_config = PointConfig.find_or_initialize_by_name_and_point 'placebet_fee',1
    point_config.save
  end

end
